package com.org.zp.ctrl.company;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.org.base.ctrl.ActivityTaskManager;
import com.org.base.ctrl.BaseActivity;
import com.org.base.util.HttpUtils;
import com.org.base.util.URLHelper;
import com.org.zp.R;
import com.org.zp.app.RecApp;

public class DeliveryResumeActivity extends BaseActivity {
	private RecApp mApp;

	private TextView headerTitle;
	private Button backbutton, top_right_btn;

	private TextView myzp_browse_resume_name, myzp_browse_resume_user_name,
			myzp_browse_resume_year, myzp_browse_resume_height,
			myzp_browse_resume_account, myzp_browse_resume_phone,
			myzp_browse_resume_email, myzp_browse_resume_address, myzp_browse_resume_sex;

	private TextView myzp_browse_resume_edu;
	private TextView myzp_browse_resume_exp;

	private Button myzp_browse_resume_bt;

	private ImageView usercenter_icon_header;
	
	private static final int CROP_FROM_CAMERA = 2;
	
	private Bitmap bitmap;
	
	private String mSex = "1";
	private String mID = "";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.myzhaopin_browse_resume);
		mApp = getMyApplication();

		if (!(ActivityTaskManager.getInstance()
				.containsName("DeliveryResumeActivity"))) {
			ActivityTaskManager.getInstance().putActivity(
					"DeliveryResumeActivity", this);
		}

		// 返回按钮
		backbutton = (Button) findViewById(R.id.back);
		backbutton.setText("  返回");
		backbutton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ActivityTaskManager.getInstance().removeActivity(
						"DeliveryResumeActivity");
			}
		});
		// 标题
		headerTitle = (TextView) findViewById(R.id.headTitle);
		headerTitle.setText("简历详情 ");

		top_right_btn = (Button) findViewById(R.id.top_right_button);
		top_right_btn.setVisibility(View.GONE);

		init();

	}

	private void init() {
		
		mID = getIntent().getStringExtra("id");

		myzp_browse_resume_name = (TextView) findViewById(R.id.myzp_browse_resume_name);
		myzp_browse_resume_user_name = (TextView) findViewById(R.id.myzp_browse_resume_user_name);
		myzp_browse_resume_year = (TextView) findViewById(R.id.myzp_browse_resume_year);
		myzp_browse_resume_height = (TextView) findViewById(R.id.myzp_browse_resume_height);
		myzp_browse_resume_account = (TextView) findViewById(R.id.myzp_browse_resume_account);
		myzp_browse_resume_phone = (TextView) findViewById(R.id.myzp_browse_resume_phone);
		myzp_browse_resume_email = (TextView) findViewById(R.id.myzp_browse_resume_email);
		myzp_browse_resume_address = (TextView) findViewById(R.id.myzp_browse_resume_address);
		myzp_browse_resume_sex = (TextView) findViewById(R.id.myzp_browse_resume_sex);

		myzp_browse_resume_edu = (TextView) findViewById(R.id.myzp_browse_resume_edu);
		myzp_browse_resume_exp = (TextView) findViewById(R.id.myzp_browse_resume_exp);

		myzp_browse_resume_bt = (Button) findViewById(R.id.myzp_browse_resume_bt);
		myzp_browse_resume_bt.setVisibility(View.GONE);

		usercenter_icon_header = (ImageView) findViewById(R.id.myzp_create_resume_usercenter_icon_header);
		
		new getResumeInfo().execute(URLHelper.mPrefix);

	}
	
	class getResumeInfo extends AsyncTask<String, Void, String> {

		@Override
		protected String doInBackground(String... params) {
			Map<String, String> map = new HashMap<String, String>(); // 登录时向服务器发送的参数
			map.put("act", "deliverResumeDetail");
			map.put("did", mID);
			return HttpUtils.readHttp(params[0], map);// 网络操作，向服务器发送登录请求，获取返回信息

		}

		@Override
		protected void onPostExecute(String result) {

			if (result == null || result.equals("")) {
				// 发送解析数据 消息
				Toast toast = Toast.makeText(DeliveryResumeActivity.this,
						"失败", Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				
			} else if (result.equals("timeout")) {
				// 发送解析数据 消息
				Toast toast = Toast.makeText(DeliveryResumeActivity.this,
						"网络超时", Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				
			} else {
				parseJson(result);
			}

		}
	}

	private void parseJson(String str) {
		try {
			String succeed = new JSONObject(str).getString("succeed");
			if (succeed.equals("000")) {
				JSONObject json = new JSONObject(str);
				
				myzp_browse_resume_name.setText(json.getString("title"));
				myzp_browse_resume_user_name.setText(json.getString("fullname"));
				if("1".equals(json.getString("sex"))){
					myzp_browse_resume_sex.setText("男");
				}else{
					myzp_browse_resume_sex.setText("女");
				}
				myzp_browse_resume_year.setText(json.getString("birthdate"));
				myzp_browse_resume_height.setText(json.getString("height"));
				myzp_browse_resume_account.setText(json.getString("householdaddress"));
				myzp_browse_resume_phone.setText(json.getString("telephone"));
				myzp_browse_resume_email.setText(json.getString("email"));
				myzp_browse_resume_address.setText(json.getString("address"));
				myzp_browse_resume_exp.setText(json.getString("experience_cn"));
				myzp_browse_resume_edu.setText(json.getString("education_cn"));
				mApp.imageLoader.DisplayImage(URLHelper.mPrefixImg + "/" + json.getString("photo_img"), usercenter_icon_header, R.drawable.icon_header);
				
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			ActivityTaskManager.getInstance().removeActivity(
					"DeliveryResumeActivity");
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
}
